package com.longcai.cm.controller;

import cn.hutool.core.date.DateUtil;
import com.longcai.cm.base.aop.Log;
import com.longcai.cm.base.bean.PageVo;
import com.longcai.cm.base.bean.Pageable;
import com.longcai.cm.bean.R;
import com.longcai.cm.common.utils.RequestUtil;
import com.longcai.cm.common.utils.StringUtil;
import com.longcai.cm.domain.JaOrder;
import com.longcai.cm.excel.ExcelUtils;
import com.longcai.cm.service.JaOrderRefundApplyService;
import com.longcai.cm.service.JaOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author LBX
 * @date 2024-07-30
 */
@RestController
@RequestMapping("/JaOrder")
public class JaOrderController {

    private final JaOrderService jaOrderService;

    private final JaOrderRefundApplyService jaOrderRefundApplyService;

    public JaOrderController(JaOrderService jaOrderService , JaOrderRefundApplyService jaOrderRefundApplyService) {
        this.jaOrderService = jaOrderService;
        this.jaOrderRefundApplyService = jaOrderRefundApplyService;
    }

    @RequestMapping("/getList")
    public R<PageVo<JaOrder>> getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JaOrder> result = jaOrderService.getPageBean(pageInfo, JaOrder.class);
        return R.success(result);
    }

    /**
     * @param vo 插入实体
     * @return R
     */
    @Log
    @RequestMapping("/save")
    public R<String> save(JaOrder vo) {
        if (StringUtils.isNotBlank(vo.getId())) {
            boolean success = jaOrderService.updateById(vo);
            return success ? R.success() : R.error();
        } else {
            final String uuid = StringUtil.getUuid();
            vo.setId(uuid);
            boolean success = jaOrderService.save(vo);
            return success ? R.success() : R.error();
        }
    }

    /**
     * @param vo      修改实体
     * @param request id
     * @return R
     */
    @Log
    @RequestMapping("/edit")
    public R<String> edit(JaOrder vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            boolean success = jaOrderService.updateById(vo);
            return success ? R.success() : R.error();
        } else {
            return R.error("id不能为空");
        }
    }

    /**
     * @param id 主键
     * @return R
     */
    @Log
    @RequestMapping("/delete/{id}")
    public R<String> deleteByPrimaryKey(@PathVariable("id") Integer id) {
        final boolean success = jaOrderService.removeById(id);
        return success ? R.success() : R.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return R
     */
    @Log
    @RequestMapping("/delete")
    public R<String> delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            final boolean success = jaOrderService.removeById(idString);
            if (!success) {
                return R.error();
            }
        }
        return R.success();
    }


    @GetMapping("exportExcelEquipment")
    public void exportExcelEquipment(HttpServletRequest request, HttpServletResponse response, String ids)throws Exception {
        List<JaOrder> jaOrders ;
        if (StringUtils.isNotBlank(ids)){
            String[] split = ids.split(",");
            List<String> list = Arrays.asList(split);
            jaOrders =  jaOrderService.listByIds(list);
        }else{
            jaOrders = jaOrderService.list();
        }
        ExcelUtils.export(response,"订单表"+ DateUtil.today(),jaOrders,JaOrder.class);
    }


    @RequestMapping("/changeTag")
    public R<String> changeTag(String ids,String type) {
        if (type.equals("1")){
            List<JaOrder> jaOrders = jaOrderService.listByIds(Arrays.asList(ids.split(",")));
            for (JaOrder jaOrder : jaOrders) {
                jaOrder.setCheckStatus(1);
                jaOrderService.updateById(jaOrder);
            }
        }else{
            List<JaOrder> jaOrders = jaOrderService.listByIds(Arrays.asList(ids.split(",")));
            for (JaOrder jaOrder : jaOrders) {
                jaOrder.setCheckStatus(0);
                jaOrderService.updateById(jaOrder);
            }
        }

        return R.success();
    }



}
